package com.blacktea.creationalpatterns.singletonpattern;

import org.springframework.stereotype.Component;

import javax.inject.Singleton;

/**
 * @description: 双重检查锁来确保线程安全,但是这样会被反射获得
 * @author: black tea
 * @date: 2023/3/19 16:15
 */
@Component
@Singleton // 用于标记一个类为单例模式。它的作用是告诉Spring框架，在整个应用程序中只需要创建一个该类的实例。
public class MySingleton2 {

    private static volatile MySingleton2 instance;

    private MySingleton2() {}

    public static MySingleton2 getInstance() {
        if (instance == null) {
            synchronized (MySingleton2.class) {
                if (instance == null) {
                    instance = new MySingleton2();
                }
            }
        }
        return instance;
    }
}
